<template>
<view>
<!-- 富文本 -->

<view class="wxParseHtmlContainer">

<!-- <view class='promotion_title'>
  <text class='promotion_name'>{{promotionData.name}}</text>
  <text class='promotion_time'>【{{promotionData.startDate}}】后开始</text>
</view> -->
<!-- <component is="init" :wxParseData="article.nodes"></component> -->
<rich-text-component :receiveData="articleJsonData" v-if="articleJsonData"></rich-text-component>
<!-- 首页组件-私有活动详情装饰-->
<custom-page-index-component :receiveData="sendPromotionData" v-if="sendIndexData"></custom-page-index-component>
<!-- 首页组件-公共活动详情装饰-->
<custom-page-index-component :receiveData="sendIndexData" v-if="sendIndexData"></custom-page-index-component>
<view v-if="promotionData.promotionType==50">
  <view class="attend_promotion" data-state="attend" @click.stop="attendPromotion" v-if="promotionData.signUp==0" :style="'background:' + setting.platformSetting.defaultColor">
    <text>参加活动</text>
  </view>
  <view class="disattend_promotion" data-state="disattend" @click.stop="attendPromotion" v-if="promotionData.signOut==0&&promotionData.signUp==1">
    <text>退出活动</text>
  </view>
</view>
</view>
</view>
</template>

<script>

// import componentCustompage from "../../view/partials/custom_page/index";

export default {
  data() {
    return {
      promotionData: null,
      sendIndexData: "",
      articleJsonData:'',
      setting: "",
      sendPromotionData: "",
      article_article: ""
    };
  },

  props: {},
  onLoad: function (options) {
    let that = this;
    that.opt = options;
    console.log("===options===", options);
    that.setData({
      setting: getApp().globalData.setting
    });
    let sendIndexData = JSON.stringify({
      title: 'noTitle',
      url: "promotion_detail",
      params: {
        pageObjectId: options.promotionId
      }
    });
    that.setData({
      sendIndexData: sendIndexData
    });
    let sendPromotionData = JSON.stringify({
      title: 'noTitle',
      url: "promotion_detail_" + options.promotionId
    });
    that.setData({
      sendPromotionData: sendPromotionData
    }); // let navName = options.navName
    // if (navName) {
    //   wx.setNavigationBarTitle({
    //     title: navName,
    //   })
    // }
    // let that = this
    // let richTextHtml = app.richTextHtml
    // WxParse.wxParse('article', 'html', richTextHtml, that, 10);

    that.getData(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  onShareAppMessage: function () {
    let that = this;
    let params = that.opt;
    console.log('params:' + params);
    return getApp().globalData.shareForFx2('promotion_detail', '', params);
  },
  methods: {
    attendPromotion: function (e) {
      console.log("===attendPromotion===", e);
      let state = e.currentTarget.dataset.state;
      let promotionUrl = "";
      let title = "";

      if (state == "disattend") {
        promotionUrl = "/wx_dis_attend_promotion.html", title = "取消成功";
      } else {
        promotionUrl = "/wx_attend_promotion.html";
        title = "参加成功";
      }

      let param = this.opt;
      let customIndex = getApp().globalData.AddClientUrl(promotionUrl, param, 'get');
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res.data);

          if (res.data.errcode == 0) {
            wx.showToast({
              title: title,
              icon: "success",
              duration: 2000
            });
          } else {
            wx.showToast({
              title: res.data.errMsg,
              image: '/wxcomponents/images/icons/tip.png',
              duration: 2000
            });
          }
        },
        fail: function (res) {
          getApp().globalData.loadFail();
        }
      });
    },
    getData: function (b) {
      var that = this;
      var param = {
        promotionId: b.promotionId
      };
      var customIndex = getApp().globalData.AddClientUrl("/get_promotions_detail.html", param, 'get');
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log(res);
          console.log(res.data);

          if (res.data.errcode == "-1") {
            wx.showToast({
              title: res.data.errMessage,
              image: '/wxcomponents/images/icons/tip.png',
              duration: 2000
            });
          } else {
            if (res.data.relateObj.content) {
              	let articleJsonData=res.data.relateObj.content;
              	that.setData({
              		articleJsonData: {data:{jsonData:{content:articleJsonData}}}
              	});
                console.log('====articleJsonData====', that.articleJsonData);
            }
            that.setData({
              promotionData: res.data.relateObj
            });

            if (res.data.relateObj.name) {
              wx.setNavigationBarTitle({
                title: res.data.relateObj.name
              });
            }

            if (res.data.relateObj.content) {
              //WxParse.wxParse('article', 'html', res.data.relateObj.content, that, 10)
              setTimeout(function () {
                that.article_article = res.data.relateObj.content;
              });
            }
          }
        },
        fail: function (res) {
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>